//
//  YSModifyInfoCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/8/6.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  修改信息

#import "YSModifyInfoCtrl.h"
#import "SelectImgUtils.h"
#import "YSUploadFileRes.h"
#import "YSMineDataTool.h"
#import "JTSActionSheet.h"
#import "UpYun.h"
#import "YSProgressImageView.h"
#import "YSBindPhoneCtrl.h"


@interface YSModifyInfoCtrl ()<SelectImgUtilsDelegate,UIActionSheetDelegate,YSProgressImageViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headImgView;
@property (weak, nonatomic) IBOutlet UITextField *nicknameTF;
@property (weak, nonatomic) IBOutlet UILabel *genderLabel;
@property (copy, nonatomic) NSString *headUrl;
@property (strong, nonatomic) UIImagePickerController *imagePickerController;
@property (strong, nonatomic) YSProgressImageView *newsHeadView;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIButton *bindBtn;

@end

@implementation YSModifyInfoCtrl

- (IBAction)tapCover:(id)sender {
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"个人信息"];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveInfo:)];
    rightItem.tintColor = ThemeColor;
    self.navigationItem.rightBarButtonItem = rightItem;
    
    self.headImgView.hidden = YES;
    self.newsHeadView = [[YSProgressImageView alloc]init];
    [self.newsHeadView setFrame:self.headImgView.frame];
    self.newsHeadView.whileShowIndicator = YES;
    [self.newsHeadView setDelegate:self];
    [self.newsHeadView setCornerRadius:32.5 borderWidth:3 borderColor:[UIColor colorWithWhite:1.0 alpha:0.6]];
    self.newsHeadView.x = SCREENW-YSPublicXInset*3-self.newsHeadView.width;
    self.newsHeadView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:self.newsHeadView];
    
    [self initData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePhoneCode:) name:kUpdatePhoneCodeNortif object:nil];
}

-(void)initData{
    NSString *headImg = [DefaultsHelper stringForKey:kHeadImg];
    NSString *nickName = [DefaultsHelper stringForKey:kNickName];
    NSInteger sex = [DefaultsHelper integerForKey:kUserGender];
    NSString *gender = @"未知";
    if(sex == GenderMan){
        gender = @"男";
    }else if(sex == GenderWoman){
        gender = @"女";
    }
    [self.genderLabel setText:gender];
    [self.newsHeadView setImageURLStr:headImg];
    [self.nicknameTF setText:nickName];
    
    [self updatePhoneCode:nil];
}

-(void)saveInfo:(UIBarButtonItem *)item{
    if(ISBLANK(self.nicknameTF.text)){
        ShowTips(@"昵称不能为空");
        return;
    }
    if(self.nicknameTF.text.length>10){
        ShowTips(@"昵称最多不可超过10个字符")
        return;
    }
    
    NSString *sex = self.genderLabel.text;
    NSInteger gender = 0;
    if([sex isEqualToString:@"男"]){
        gender = 1;
    }else if([sex isEqualToString:@"女"]){
        gender = 2;
    }
    [YSMineDataTool updateUserInfo:self.headUrl nickName:self.nicknameTF.text userGender:gender success:^(BaseResponse *result) {
        if(result.isSucc){
            [[NSNotificationCenter defaultCenter] postNotificationName:kUpdateMineInfoNortif object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            ShowEnterTips(result.data);
        }
    } failure:^(NSError *error) {
        ShowTips(kRequestFailure);
    }];
}

- (IBAction)tapAction:(UIButton *)sender {
    switch (sender.tag) {
        case 0://修改头像
        {
            SelectImgUtils *selectImgUtils = [SelectImgUtils shareInstance];
            [selectImgUtils selectImageWithCtrl:self];
            selectImgUtils.maxSelectCount = 1;
            [selectImgUtils setDelegate:self];
            break;
        }
        case 1://修改昵称
            [self.nicknameTF becomeFirstResponder];
            break;
        case 2: //修改性别
        {
            [self chooseSex];
           break;
        }
        case 3:{ //绑定手机号
            CustomNavigationCtrl *nav = [[CustomNavigationCtrl alloc]initWithRootViewController:[[YSBindPhoneCtrl alloc]init]];
            [self.navigationController presentViewController:nav animated:YES completion:^{}];
            break;
        }
        default:
            break;
    }
}

-(void)updatePhoneCode:(NSNotification *)nortif{
    NSString *phoneCode = [DefaultsHelper stringForKey:kUserPhone];
    
    if (!ISBLANK(phoneCode)) {
        self.phoneLabel.hidden = NO;
        self.bindBtn.hidden = YES;
        self.phoneLabel.text = [phoneCode stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    }else{
        self.phoneLabel.hidden = YES;
        self.bindBtn.hidden = NO;
    }
}

-(void)selectImgUtilsImg:(UIImage *)img{
    [self.newsHeadView uploadImg:img];
}

-(void)uploadImageSuccess:(NSString *)imageUrl tag:(NSInteger)tag{
    self.headUrl = imageUrl;
}

-(void)chooseSex{
    [self.view endEditing:YES];
    
    JTSActionSheetTheme *theme = [JTSActionSheetTheme defaultTheme];
    theme.separatorColor = RGBColor(204, 204, 204);
    theme.normalButtonColor = ThemeColor;
    theme.destructiveButtonColor = [UIColor blackColor];
    theme.titleColor = [UIColor blackColor];
    
    JTSActionSheetItem *man = [JTSActionSheetItem itemWithTitle:@"男" action:^{
        [self.genderLabel setText:@"男"];
    } isDestructive:NO];
    
    JTSActionSheetItem *woman = [JTSActionSheetItem itemWithTitle:@"女" action:^{
        [self.genderLabel setText:@"女"];
    } isDestructive:NO];
    JTSActionSheetItem *other = [JTSActionSheetItem itemWithTitle:@"未知" action:^{
        [self.genderLabel setText:@"未知"];
    } isDestructive:NO];
    JTSActionSheetItem *cancel = [JTSActionSheetItem itemWithTitle:@"取消" action:^{
    } isDestructive:NO];
    
    JTSActionSheet *sheet = [[JTSActionSheet alloc] initWithTheme:theme
                                                            title:nil
                                                      actionItems:@[man, woman,other]
                                                       cancelItem:cancel];
    [sheet showInView:self.view];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:kUpdatePhoneCodeNortif object:nil];
}

@end
